﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class EventsNotifyController :UI.BaseController
    {
        public const string ProgrameCode = "events";
        public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public EventsNotifyController()
        {
            ViewBag.CurrentPath = currentPath;
        }

        [UI.ValidateAuthority(ProgrameCode , "view_notify")]
        public ActionResult Index(string EventCode)
        {
            ViewBag.EventCode = EventCode;
            return View();
        }

        [UI.ValidateAuthority(ProgrameCode , "view_notify")]
        public string List(int page, int rows, string sort, string order)
        {
            string where = Common.HqlQeuryHelp.GetWhere(typeof(Model.EventsNotify), "EventCode,UserName");
            string orderby = "";
            if (!string.IsNullOrEmpty(sort)) orderby = sort + " " + order;

            var bll = new BLL.EventsNotify();
            var list = bll.Get(where, orderby, (page - 1) * rows, rows);
            List<object> listView = new List<object>();
            if (list.Count > 0)
            {
                foreach (var i in list)
                {
                    listView.Add(new
                    {
                        EventCode=i.EventCode,
                        UserName = i.UserName,
                        GroupName=i.Manager.Group==null?null:i.Manager.Group.GroupName,
                        RealName = i.Manager.RealName,
                        Email=i.Manager.Email                         
                    });
                }
            }
            var ret = new
            {
                total = bll.GetCount(where),
                rows = listView
            };
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "delete_notify")]
        public string Delete(string EventCode,string id)
        {
            var ret = new BLL.EventsNotify().Delete(EventCode, id);
            return UI.Utils.ToJsonString(ret);
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "add_notify")]
        public string Add(string EventCode,string UserName )
        {
            var ret = new BLL.EventsNotify().Add(EventCode,UserName);
            return UI.Utils.ToJsonString(ret);
            
        }
    }
}
